package cn.rui.controller;

import cn.rui.constant.MessageConstant;
import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.entity.Result;
import cn.rui.pojo.Member;
import cn.rui.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 会员管理
 * @author 徽州大都督
 * @date 2020/9/1
 */
@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 分页带条件查询查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @PreAuthorize("hasAnyAuthority('MEMBER_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return memberService.findPage(queryPageBean);

    }

    /**
     * 添加会员
     * @param member
     * @return
     */
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('MEMBER_ADD')")
    public Result add(@RequestBody Member member){


        try {
            memberService.add(member);
            return new Result (true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 根据id删除会员
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    @PreAuthorize("hasAnyAuthority('MEMBER_DELETE')")
    public Result delete(@PathVariable("id")Integer id){
        try {
            memberService.deleteById(id);
            return new Result (true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.DELETE_MEMBER_FAIL);
        }

    }
}
